package com.longrise.android.byjk.plugins.tabfirst.news;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.webkit.WebView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.tabfirst.commentdialog.ComDialog;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;
import com.longrise.common.base.web.BaseWebClient;

/**
 * Created by callxkj on 2017/10/18.
 * 新闻详情 使用新的basewebactivity
 */

public class NewsDetail2Activity extends BaseWebActivity {

    public static final String EXTRA_NEW_URL = "EXTRA_NEW_URL";
    private String mUrl ;

    private WebView mWv;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return  R.layout.activity_newsdetail2;
    }

    @Override
    public WebView getWebView() {
        return mWv;
    }

    @Override
    public void init() {
        mWv = (WebView) findViewById(R.id.newsdetail2_wv);
    }

    @Override
    public void initData() {
        setWebTitle(getString(R.string.by_zixun));
        getExtraData();
        loadUrl(mUrl);
    }

    @Override
    public BaseWebBridge getwebBridge() {
        return new NewsDetail2Bridge(this,mRxmanager);
    }

    @Override
    public BaseWebClient getWebClient() {
        return new BaseWebClient(this);
    }

    /**
     * 获取传过来的地址
     */
    private void getExtraData() {
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra(EXTRA_NEW_URL)) {
            String url = intent.getStringExtra(EXTRA_NEW_URL);
            StringBuilder urlBuilder = new StringBuilder(url);
            urlBuilder.append("&cardno=" + UserInfor.getInstance().getUsersfzh());
            urlBuilder.append("&userid=" + UserInfor.getInstance().getUserid());
            mUrl = urlBuilder.toString();
        }
    }

    /**
     * 评论dialog
     */
    public void showComDialog() {
        ComDialog comDialog = new ComDialog(this, new ComDialog.MsgCallback() {
            @Override
            public void msg(String msg) {
                if (!TextUtils.isEmpty(msg)) {
                    //将参数传递给web
                    mWv.loadUrl("javascript:$_$.commitAnswer('" + msg + "')");
                }
            }
        });
        comDialog.show();
    }
}
